<?php

App::uses('AppController', 'Controller');

/**
 * Scors Controller
 *
 * @property Scor $Scor
 */
class ScorsController extends AppController {

    /**
     * index method
     *
     * @return void
     */
    public $layout = 'hospital';
    public $uses = array('Scor', 'Parametroscor');

    /**
     * index method
     *
     * @return void
     * 
     */
    public function beforeFilter() {
        parent::beforeFilter();
        //$this->Auth->allow();
    }

    public function index() {
        $scors = $this->Scor->find('all', array('recursive' => -1));
        $this->set(compact('scors'));
    }

    /**
     * view method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function view($id = null) {
        if (!$this->Scor->exists($id)) {
            throw new NotFoundException(__('Invalid scor'));
        }
        $options = array('conditions' => array('Scor.id' => $id));
        $tabla = $this->Scor->find('first', $options);
        //debug($tabla);exit;
        $this->set(compact('tabla'));
    }

    /**
     * add method
     *
     * @return void
     */
    public function add() {
        if ($this->request->is('post')) {
            $this->Scor->create();
            if ($this->Scor->save($this->request->data)) {
                $this->Session->setFlash(__('The scor has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The scor could not be saved. Please, try again.'));
            }
        }
    }

    /**
     * edit method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function edit($id = null) {
        $this->Scor->id=$id;
        if (!$this->Scor->exists($id)) {
            throw new NotFoundException(__('Invalid scor'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->Scor->save($this->request->data)) {
                $this->Session->setFlash(__('The scor has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The scor could not be saved. Please, try again.'));
            }
        } else {
            $options = array('conditions' => array('Scor.' . $this->Scor->primaryKey => $id));
            $this->request->data = $this->Scor->find('first', $options);
        }
    }

    /**
     * delete method
     *
     * @throws NotFoundException
     * @throws MethodNotAllowedException
     * @param string $id
     * @return void
     */
    public function delete($id = null) {
        $this->Scor->id = $id;
        if (!$this->Scor->exists()) {
            throw new NotFoundException(__('Invalid scor'));
        }
        $this->request->onlyAllow('post', 'delete');
        $parametros = $this->Parametroscor->find('count', array('conditions' => array('Parametroscor.scor_id' => $id)));
        if ($parametros == 0) {

            if ($this->Scor->delete()) {
                $this->Session->setFlash(__('CORRECTO: Se elimino la tabla'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('ERROR: No se pudo eliminar la tabla'));
                $this->redirect(array('action' => 'index'));
            }
        } else {
            $this->Session->setFlash(__('ERROR: No se pudo eliminar la tabla porque contiene parametros'));
            $this->redirect(array('action' => 'index'));
        }
    }

    public function parametros($id = null) {

        if (!empty($this->request->data)) {
            //debug($this->request->data);exit;
            if ($this->Parametroscor->save($this->request->data)) {
                //debug($this->request->data);exit;
                $idTabla = $this->request->data['Parametroscor']['scor_id'];
                $this->Session->setFlash('EXITO: Datos guardados correctamente');
                $this->redirect(array('action' => 'parametros', $idTabla));
            } else {
                $this->Session->setFlash('ERROR:  no se pudo guardar');
                $this->redirect(array('action' => 'parametros', $idTabla));
            }
        }
        $parametros = $this->Parametroscor->find('all', array(
            'conditions' => array('Parametroscor.scor_id' => $id),
            'order' => array('Parametroscor.orden ASC')
        ));
        //debug($parametros);exit;
        $dato = $this->Scor->find('first', array(
            'conditions' => array('Scor.id' => $id)
        ));
        $nombre = $dato['Scor']['nombre'];
        //debug($id);exit;
        $this->set(compact('parametros', 'id', 'nombre'));
    }

    public function ajaxcargaedit($id = null, $idTabla = null) {
        //debug($idTabla);exit;
        $this->layout = 'ajax';
        $this->Parametroscor->id = $id;
        if (!$id) {
            $this->Session->setFlash('ERROR: al guardar!!');
            $this->redirect(array('action' => 'parametros', $idTabla));
        }
        if (!empty($this->request->data)) {
            if ($this->Parametroscor->save($this->request->data)) {
                //debug($this->request->data);
                $idTabla = $this->request->data['Parametroscor']['scor_id'];
                $this->Session->setFlash('Se guardaron los cambios correctamente!!');
                $this->redirect(array('action' => 'parametros', $idTabla));
            } else {
                $this->Session->setFlash('ERROR: al guardar!!');
                $this->redirect(array('action' => 'parametros', $idTabla));
            }
        } else {
            $options = array('conditions' => array('Parametroscor.' . $this->Parametroscor->primaryKey => $id));
            $this->request->data = $this->Parametroscor->find('first', $options);
            $this->set(compact('idTabla'));
        }
    }

    public function eliminaparametro($id = null, $idTabla = null) {
        if ($this->Parametroscor->delete($id)) {
            $this->Session->setFlash('Se guardaron los cambios correctamente!!');
            $this->redirect(array('action' => 'parametros', $idTabla));
        } else {
            $this->Session->setFlash('Se guardaron los cambios correctamente!!');
            $this->redirect(array('action' => 'parametros', $idTabla));
        }
    }

}
